package com.adeoservices.gwt.dispatch.spring.sample.client;

import net.customware.gwt.dispatch.client.DispatchAsync;

import com.adeoservices.gwt.dispatch.spring.client.SpringDispatchServiceAsyncImpl;
import com.adeoservices.gwt.dispatch.spring.sample.client.allowed.AllowedAction;
import com.adeoservices.gwt.dispatch.spring.sample.client.allowed.AllowedResult;
import com.adeoservices.gwt.dispatch.spring.sample.client.denied.DeniedAction;
import com.adeoservices.gwt.dispatch.spring.sample.client.denied.DeniedResult;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class SampleEntryPoint implements EntryPoint {

	public void onModuleLoad() {
		
		GWT.log("yo !", null);
		
		VerticalPanel mainPanel = new VerticalPanel();
		
		// Better with GIN injection
		final DispatchAsync dispatchAsync = GWT.create(SpringDispatchServiceAsyncImpl.class);
		
		Button b1 = new Button("Click me : should be OK");
		mainPanel.add(b1);
		b1.addClickHandler(new ClickHandler(){

			public void onClick(ClickEvent event) {
				dispatchAsync.execute( new AllowedAction(), new AsyncCallback<AllowedResult>() {
		            public void onFailure( Throwable e ) {
		                Window.alert( "Error: " + e.getMessage() );
		            }

		            public void onSuccess( AllowedResult result ) {
		                Window.alert( "result is OK" );
		            }

		        } );
			}});
		Button b2 = new Button("Click me : access should be denied");
		mainPanel.add(b2);
		b2.addClickHandler(new ClickHandler(){

			public void onClick(ClickEvent event) {
				dispatchAsync.execute( new DeniedAction(), new AsyncCallback<DeniedResult>() {
		            public void onFailure( Throwable e ) {
		                Window.alert( "Error: " + e.getMessage() );
		            }

		            public void onSuccess( DeniedResult result ) {
		                Window.alert( "result is OK : bad, shoud be denied" );
		            }

		        } );
			}});
		
		RootPanel.get().add(mainPanel);
	}

}
